package inet.app.view.slidemenu;

import inet.app.R;
import inet.app.view.slidemenu.SlideMenuAnimationContainer.MenuItemSelectedAction;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class SlideMenu implements SlideMenuAnimationContainer.Listener {

//	public final static String[] slideMenuOptions = { "Activity 1", "Activity 2", "Activity 3" };
	public final static String TAG = "SlideMenu";
//	protected static final String TextView = null;

	private Context context;
	private SlideMenuAnimationContainer slideMenuAnimationContainer;

	public SlideMenu( Context context, SlideMenuAnimationContainer mainAnimationLayout){

		this.context = context;
		this.slideMenuAnimationContainer = mainAnimationLayout;
	}

	public void init(List<SlideMenuItem> slideMenuList){

		final Activity activity = (Activity) context;

		//Set Content's show menu button's action
		ImageView showMenuButton = (ImageView) activity.findViewById( R.id.content_button);
		showMenuButton.setOnClickListener( new OnClickListener(){
			@Override
			public void onClick( View v){
				slideMenuAnimationContainer.toggleSlideMenu();
			}
		});

		SlideMenuArrayAdapter slideMenuArrayAdaptar = new SlideMenuArrayAdapter( context, android.R.layout.simple_list_item_1, slideMenuList);
		
		ListView menuListView = (ListView) activity.findViewById( R.id.slideMenuListView);
		menuListView.setAdapter( slideMenuArrayAdaptar);
		menuListView.setOnItemClickListener( new OnItemClickListener(){

			@Override
			public void onItemClick( AdapterView<?> parent, View view, int position, long id){

				// Close sidebar
				slideMenuAnimationContainer.closeSlideMenuAndActOnClick( parent, view, position, id);
			}
		});
		
		//Implement your logic here within the execute() function when an item is clicked with in the SlideMenu
		//Called after the SlideMenu collapses.
		slideMenuAnimationContainer.setMenuItemSelectedAction( new MenuItemSelectedAction(){

			@Override
			public void execute( AdapterView<?> parent, View view, int position, long id){

				//Only act when a ListViewItem with type Activity is clicked
//				TextView textView = (TextView)view.findViewById( R.id.MenuActivityListViewItem_text);
//				if( textView != null && textView.getId() == R.id.MenuActivityListViewItem_text){
//					
//					//Start new activity
//					CharSequence selectedActivityName = textView.getText();
//	
//					Intent intent;
//					if( selectedActivityName.equals( "Activity 1")) {
//						intent = new Intent( activity, Live.class);
//					}
//					else if( selectedActivityName.equals( "Activity 2")) {
//						intent = new Intent( activity, Activity2.class);
//					}
//					else {
//						intent = new Intent( activity, Activity3.class);
//					}
//					intent.setFlags( Intent.FLAG_ACTIVITY_NO_ANIMATION);
//					activity.startActivity( intent);
//				}
			}
		});
	}

	//Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
	@Override
	public void onSlideMenuOpened(){
		Log.d( TAG, "opened");
	}

	//Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
	@Override
	public void onSlideMenuClosed(){
		Log.d( TAG, "closed");
	}

	//Callback of SlideMenuAnimationContainer.Listener to monitor status of SlideMenu
	@Override
	public boolean onContentTouchedWhenOpening(){
		//The content area is touched when sidebar opening, close sidebar
		Log.d( TAG, "going to close sidebar");
		slideMenuAnimationContainer.closeSlideMenu();
		return true;
	}
}
